// Copyright 2013 by Rod Moten
//
// This program is part of the Grambler project.
// 
// Grambler is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as 
// published by the Free Software Foundation, either version 3 of 
// the License, or (at your option) any later version.
//
// Grambler is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Grambler.  If not, see <http://www.gnu.org/licenses/>..
package edu.hendrix.grambler;

/**
 * @author profmocs@gmail.com
 *
 */
class ActionResultImpl implements ActionResult {
	private Throwable exception;
	private Object result;

	static ActionResult execute(Action action, Tree tree) {
		ActionResultImpl actionResult = new ActionResultImpl();
		try {
			Object result = action.execute(tree);
			
			actionResult.result = result;
		} catch (Throwable e) {
			actionResult.exception = e;
		}
		return actionResult;
	}
	
	private ActionResultImpl() {
		
	}


	/* (non-Javadoc)
	 * @see edu.hendrix.grambler.ActionResult#hasException()
	 */
	@Override
	public boolean hasException() {
		return exception != null;
	}

	/* (non-Javadoc)
	 * @see edu.hendrix.grambler.ActionResult#getException()
	 */
	@Override
	public Throwable getException() {
		// TODO Auto-generated method stub
		return exception;
	}

	/* (non-Javadoc)
	 * @see edu.hendrix.grambler.ActionResult#getResult()
	 */
	@Override
	public Object getResult() {
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return String.format("ActionResult [exception=%s, result=%s]",
				exception, result);
	}

	
}
